<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1" import="Connect.*" %>

<%
	// Redirect to the home page if the user is not a manager or employee.
	if (session.getAttribute("user_type") == null) {
		response.sendRedirect("no_permission.jsp");
	} else if (!session.getAttribute("user_type").equals("0")
			&& !session.getAttribute("user_type").equals("1")) {
		response.sendRedirect("no_permission.jsp");
	}
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<link rel="stylesheet" type="text/css" href="style.css" media="screen" />
<title>Hammer Time Auctions</title>
<script language="javascript" type="text/javascript">
	function registerClicked() {
		javascript: edit_item.submit();
	}
</script>
</head>
<body>
	<div id="content">
		<%@include file="header.jsp"%>
		<%
			String id = request.getParameter("id");
			if (id == null) {
				id = (String) session.getAttribute("id");
			}
		%>
		<h1>
			Edit An Item - ID:
			<%=id%></h1>
		<hr></hr>
		<div class="sidebar right margin-5px padding-5px">
			<%
				// Display the list of registration errors if there are any.
				if (session.getAttribute("register_errors") != null) {
					out.println("<div class=\"section padding-5px error-text\">");
					out.println(session.getAttribute("register_errors"));
					out.println("</div>");
				}
				// If there are no register errors, display the database values.
				if (session.getAttribute("register_errors") == null) {
					// Information required for opening a connection with the database.
					String myJDBCDriver = "com.mysql.jdbc.Driver";
					String myURL = "jdbc:mysql://localhost/htah_database";
					String myUserID = "root";
					String myPassword = "hello";

					// Getting information and updating the database.
					java.sql.Connection conn = null;
					try {
						// Connect to the database.
						Class.forName(ConnectionInfo.myJDBCDriver()).newInstance();
						java.util.Properties sysprops = System.getProperties();
						sysprops.put("user", ConnectionInfo.myUserID());
						sysprops.put("password", ConnectionInfo.myPassword());
						conn = java.sql.DriverManager.getConnection(ConnectionInfo.myURL(), sysprops);

						// Retrieve item data.
						java.sql.Statement statement = conn.createStatement();
						java.sql.ResultSet rs = statement.executeQuery("SELECT * FROM items WHERE id = '" + id + "'");
						rs.next();
						session.setAttribute("name", rs.getString("name"));
						session.setAttribute("year", rs.getString("year"));
						session.setAttribute("type", rs.getString("type"));
						session.setAttribute("description", rs.getString("description"));
						session.setAttribute("copies_sold", rs.getString("copies_sold"));
						session.setAttribute("copies_available", rs.getString("copies_available"));
					} catch (Exception e) {
						e.printStackTrace();
						out.print(e.toString());
					} finally {
						try {
							conn.close();
						} catch (Exception ee) {
						}
					}
				}

				// Check if there were previously entered values for the form. If there aren't then use an
				// empty string.
				if (session.getAttribute("name") == null) {
					session.setAttribute("name", "");
				}
				if (session.getAttribute("year") == null) {
					session.setAttribute("year", "");
				}
				if (session.getAttribute("type") == null) {
					session.setAttribute("type", "");
				}
				if (session.getAttribute("description") == null) {
					session.setAttribute("description", "");
				}
				if (session.getAttribute("copies_sold") == null) {
					session.setAttribute("copies_sold", "");
				}
				if (session.getAttribute("copies_available") == null) {
					session.setAttribute("copies_available", "");
				}
			%>
		</div>
		<%
			// Create a back button.
			out.println("<a href=\"view_items.jsp\" class=\"margin-5px button\">Back</a>");
		%>
		<div class="center" id="register-box">
			<form name="edit_item" action="edit_item_validate.jsp" method="post"
				enctype="multipart/form-data">
				<table>
					<tr>
						<td><label class="form-label" for="name">Name:</label></td>
						<td><input name="name" type="text"
							value="<%=session.getAttribute("name")%>" /></td>
					</tr>
					<tr>
						<td><label class="form-label" for="year">Year:</label></td>
						<td><input name="year" type="text"
							value="<%=session.getAttribute("year")%>" /></td>
					</tr>
					<tr>
						<td><label class="form-label" for="type">Type:</label></td>
						<td><input name="type" type="text"
							value="<%=session.getAttribute("type")%>" /></td>
					</tr>
					<tr>
						<td><label class="form-label" for="description">Description:</label></td>
						<td><textarea name="description" rows="6" cols="35"><%=session.getAttribute("description")%></textarea></td>
					</tr>
					<tr>
						<td><label class="form-label" for="copies_sold">Copies Sold:</label></td>
						<td><input name="copies_sold" type="text"
							value="<%=session.getAttribute("copies_sold")%>" /></td>
					</tr>
					<tr>
						<td><label class="form-label" for="copies_available">Copies
								Available:</label></td>
						<td><input name="copies_available" type="text"
							value="<%=session.getAttribute("copies_available")%>" /></td>
					</tr>
					<tr>
						<td><label class="form-label" for="picture">Picture: </label></td>
						<td><input name="picture" type="file" accept="image/*" /></td>
					</tr>
					<tr>
						<td></td>
						<td><a href="#" class="left button" type="submit"
							onclick="return registerClicked()">Add Item</a></td>
					</tr>
				</table>
				<input name="id" class="hidden" value="<%=id%>" /> <input type="submit"
					class="hidden" />
			</form>
		</div>
	</div>
	<%
		// Clear all the attributes after they have been read.
		session.setAttribute("register_errors", null);
		session.setAttribute("id", null);
		session.setAttribute("name", null);
		session.setAttribute("type", null);
		session.setAttribute("year", null);
		session.setAttribute("description", null);
		session.setAttribute("copies_available", null);
		session.setAttribute("copies_sold", null);
	%>
	<hr></hr>
	<%@include file="footer.jsp"%>
</body>
</html>